package com.jing.cloud.service.user.method;

import com.google.common.collect.Lists;
import com.jing.cloud.service.method.AbstractMethod;
import com.jing.cloud.service.method.Method;
import com.jing.cloud.service.user.bean.Account;
import com.jing.cloud.service.user.bean.User;
import com.jing.cloud.service.user.dao.AccountDao;
import com.jing.cloud.service.user.dao.UserDao;
import com.jing.cloud.service.user.method.param.GetUserBean;
import com.jing.cloud.service.Res;
import com.jing.cloud.util.RspUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by 29017 on 2017/9/14.
 */
@Component
public class GetUsers extends AbstractMethod<GetUserBean> implements Method<GetUserBean> {

    @Autowired
    private UserDao userDao;
    @Autowired
    private AccountDao accountDao;

    @Override
    public Res call(GetUserBean bean) throws Exception {
        List<User> users = userDao.findByIds(bean.getUserIds());
        if (null != users && !users.isEmpty() && bean.isWithAccount()) {
            List<Account> accounts = accountDao.getListByUserIds(bean.getUserIds());
            if (null != accounts && !accounts.isEmpty()) {
                for (Account account : accounts) {
                    for (User user : users) {
                        if (account.getUserId().equals(user.getId())) {
                            if (user.getAccounts() == null) {
                                user.setAccounts(Lists.<Account>newArrayList());
                            }
                            user.getAccounts().add(account);
                        }
                    }
                }
            }
        }
        return RspUtil.success(users);
    }
}
